<?php 

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品原材料
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/goods/goodsMater.class.php');

class ajax_goodsMater {
	
	public function execute() {
		
		if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] >0) {
			
			switch ($_REQUEST['operFlg']) {
				case 1:	//商品原材料列表
					$this->goodsMaterList();
					break;
				case 2:	//保存原材料信息
					$this->goodsMaterSave();
					break;
				case 3:	//获取添加时原材料的最大序号
					$this->maxSort();
					break;
				case 4:	//获取修改时所需的信息
					$this->getgoodsMater();
					break;
				case 5: //删除
					$this->goodsMaterDel();
					break;
				case 6:	//使用标志
					$this->goodsMaterChange();
					break;
				default:
					backendError();
			}
			
		} else {
			backendError();
		}
		
	}
	
	/**
	 * 商品原材料列表
	 */
	private function goodsMaterList() {
		
		global $G_PGLOB;
		
		$goodsMater = new goodsMater($G_PGLOB->DBCA);
		
		$goodsMaterInfo = $goodsMater->searchMater($_REQUEST);
		
		$result_array = array(
				'page'  => $_REQUEST['page'] ,
				'total' => $_REQUEST['count'] ,
				'rows'  => array()
		);
		
		if ($goodsMaterInfo) {

            $edit_permission = $G_PGLOB->has_permission(535);
		
			foreach($goodsMaterInfo as &$s_l) {
		
				if ($edit_permission) {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['materID'],
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick="goodsMater.edit(' . $s_l['materID'] . ',1)">维护图片</a>&nbsp;&nbsp;&nbsp;&nbsp;'.
                            '<a href="javascript: void(0);" onclick="goodsMater.edit(' . $s_l['materID'] . ',0)">维护值</a>' ,
                            $s_l['sortOrder'],
                            $s_l['materName'],
                            $s_l['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick="goodsMater.doAction(' . $s_l['materID'] . ', 0) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="goodsMater.doAction(' . $s_l['materID'] . ', 1 ) " >禁用</a>'
                        )

                    );
                } else {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['materID'],
                        'cell' => array(
                            $s_l['materID'],
                            $s_l['sortOrder'],
                            $s_l['materName'],
                            $s_l['useFlg'] == 1 ?
                                '启用' :
                                '禁用'
                        )

                    );
                }
			}
		}
		
		echo json_encode( $result_array );
		
	}
	
	/**
	 * 保存
	 */
	private function goodsMaterSave() {
				
		global $G_PGLOB;
		
		$goodsMater = new goodsMater($G_PGLOB->DBCA);
		$retArr = $goodsMater->savegoodsMater($_REQUEST);
		
		if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '数据保存成功' , 'data' => $retArr['data'] )));
        }
		
	}
	
	/**
	 * 获取添加时原材料的最大序号
	 */
	private function maxSort() {
		
		global $G_PGLOB;
		
		$goodsMater = new goodsMater($G_PGLOB->DBCA);
		$maxSort = $goodsMater->maxSort() + 1;
				
		echo json_encode($maxSort);
		
	}
	
	/**
	 * 删除
	 */
	private function goodsMaterDel() {
		global $G_PGLOB;

        //接受要删除的参数
        $materID = $_REQUEST['p_array'][0];

        $goodsMater = new goodsMater($G_PGLOB->DBCA);
        $retArr = $goodsMater->delgoodsMater($materID);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '商品原材料删除成功' )));
        }
	}
	
	/**
	 * 使用标志
	 */
	private function goodsMaterChange() {
		
		global $G_PGLOB;
		
		$materID = $_REQUEST['materID'];
		$useFlg = $_REQUEST['useFlg'];
		
		$goodsMater = new goodsMater($G_PGLOB->DBCA);
		$retArr = $goodsMater->changeUse($materID, $useFlg);
		
		if ($retArr['code'] != 200) {
			echo json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] ));
		} else {
			echo json_encode(array( 'code' => 200 , 'message' => '使用标志修改成功' ));
		}
		
	}
	
	/**
	 * 获取修改时所需的信息
	 */
	private function getgoodsMater() {
		
		global $G_PGLOB;
		
		if (!isset($_REQUEST['materID']) || !is_numeric($_REQUEST['materID'])) {
			die(json_encode(array( 'code' => 400 , 'message' => '需要修改的商品原材料不存在' )));
		}
		
		$goodsMater = new goodsMater($G_PGLOB->DBCA);
		$goodsMaterInfo = $goodsMater->searchMater($_REQUEST);
		
		if ($goodsMaterInfo) {
			foreach ($goodsMaterInfo as $k=>$v) {
				$v['image'] = G_IMAGE_URL.$v['image'];
			}
		}
		
		echo json_encode($goodsMaterInfo[0]);
		
	}
}